package gmusic.mb.services.impl;

import gmusic.mb.bo.ArtistMBBean;
import gmusic.mb.bo.ReleaseMBBean;
import gmusic.mb.bo.TrackMBBean;
import gmusic.mb.dao.impl.MusicBrainzDAOImpl;
import gmusic.mb.services.MusicBrainzServices;

import java.io.IOException;
import java.sql.SQLException;
import java.util.List;

import org.dom4j.DocumentException;

public class MusicBrainzServicesImpl implements MusicBrainzServices {

	private static MusicBrainzServices instance = null;

	private MusicBrainzServicesImpl() {
	}

	public static MusicBrainzServices getInstance() {

		if (instance == null) {
			instance = new MusicBrainzServicesImpl();
		}
		return instance;
	}

	public ArtistMBBean getAllArtistDataFromWebServices(String ambid)
			throws SQLException, IOException, DocumentException {
		ArtistMBBean artist = MusicBrainzWebServices.getInstance().getArtist(
				ambid);
		List<ReleaseMBBean> releases = MusicBrainzWebServices.getInstance()
				.getReleases(ambid);
		for (int i = 0; i < releases.size(); i++) {
			releases.get(i).setArtistMbid(artist.getMbid());
			releases.get(i).setTracksMB(
					MusicBrainzWebServices.getInstance().getTracks(
							releases.get(i).getMbid()));
		}

		artist.setReleasesMB(releases);
		return artist;
	}

	public ArtistMBBean getAllArtistDataFromWebServicesAndSave(String ambid)
			throws SQLException, IOException, DocumentException {
		ArtistMBBean artist = getAllArtistDataFromWebServices(ambid);
		persistArtistAndAllReleasesAndTracks(artist);
		return artist;
	}

	public List<ArtistMBBean> getAllArtists() throws SQLException {

		return MusicBrainzDAOImpl.getInstance().getAllArtists();
	}

	public ArtistMBBean getArtist(String ambid) throws SQLException {
		return MusicBrainzDAOImpl.getInstance().getArtist(ambid);
	}

	public ArtistMBBean getArtistFromWebServices(String ambid)
			throws SQLException, IOException, DocumentException {
		return MusicBrainzWebServices.getInstance().getArtist(ambid);
	}

	public int getNbArtists() throws SQLException {
		return MusicBrainzDAOImpl.getInstance().getNbArtists();
	}

	public int getNbReleases() throws SQLException {
		return MusicBrainzDAOImpl.getInstance().getNbReleases();
	}

	public int getNbTracks() throws SQLException {
		return MusicBrainzDAOImpl.getInstance().getNbTracks();
	}

	public List<String> getPremieresLettreArtistes() throws SQLException {
		return MusicBrainzDAOImpl.getInstance().getPremieresLettreArtistes();
	}

	public ReleaseMBBean getReleaseAndTracks(String rmbid) throws SQLException {
		ReleaseMBBean release = MusicBrainzDAOImpl.getInstance().getRelease(
				rmbid);
		release.setTracksMB(MusicBrainzDAOImpl.getInstance().getTracks(rmbid));
		return release;
	}

	public List<ReleaseMBBean> getReleases(String ambid) throws SQLException {
		return MusicBrainzDAOImpl.getInstance().getReleases(ambid);
	}

	public List<ReleaseMBBean> getReleasesAndTracks(String ambid)
			throws SQLException {
		List<ReleaseMBBean> releases = MusicBrainzDAOImpl.getInstance()
				.getReleases(ambid);
		for (int i = 0; i < releases.size(); i++) {
			releases.get(i).setTracksMB(
					MusicBrainzDAOImpl.getInstance().getTracks(
							releases.get(i).getMbid()));
		}
		return releases;
	}

	public List<TrackMBBean> getTracks(String rmbid) throws SQLException {
		return MusicBrainzDAOImpl.getInstance().getTracks(rmbid);
	}

	public List<TrackMBBean> getTracksFromWebServices(String rmbid)
			throws SQLException, IOException, DocumentException {
		return MusicBrainzWebServices.getInstance().getTracks(rmbid);
	}

	public boolean persistArtist(ArtistMBBean ab) throws SQLException {
		return MusicBrainzDAOImpl.getInstance().persist(ab);
	}

	public boolean persistArtistAndAllReleasesAndTracks(ArtistMBBean ab)
			throws SQLException {

		MusicBrainzDAOImpl.getInstance().persist(ab);
		for (int i = 0; i < ab.getReleasesMB().size(); i++) {
			ReleaseMBBean release = ab.getReleasesMB().get(i);
			MusicBrainzDAOImpl.getInstance().persist(release);
			for (int j = 0; j < release.getTracksMB().size(); j++) {
				MusicBrainzDAOImpl.getInstance().persist(
						release.getTracksMB().get(j));
			}
		}

		return true;
	}

	public boolean persistRelease(ReleaseMBBean rb) throws SQLException {
		return MusicBrainzDAOImpl.getInstance().persist(rb);
	}

	public boolean persistReleasesAndAllTracks(ReleaseMBBean rb)
			throws SQLException {
		MusicBrainzDAOImpl.getInstance().persist(rb);
		for (int i = 0; i < rb.getTracksMB().size(); i++) {
			MusicBrainzDAOImpl.getInstance().persist(rb.getTracksMB().get(i));
		}
		return true;
	}

	public boolean persistTrack(TrackMBBean tb) throws SQLException {
		return MusicBrainzDAOImpl.getInstance().persist(tb);
	}

	public List<ArtistMBBean> rechercherArtistes(String nom)
			throws SQLException {
		return MusicBrainzDAOImpl.getInstance().rechercherArtistes(nom);
	}

	public List<ReleaseMBBean> rechercherReleases(String nom)
			throws SQLException {
		return MusicBrainzDAOImpl.getInstance().rechercherReleases(nom);
	}

	public List<TrackMBBean> rechercherTracks(String nom) throws SQLException {
		return MusicBrainzDAOImpl.getInstance().rechercherTracks(nom);
	}

	public void supprimerArtiste(String ambid) throws SQLException {
		MusicBrainzDAOImpl.getInstance().supprimerArtiste(ambid);
	}

	public ReleaseMBBean getRelease(String rmbid) throws Exception {

		return MusicBrainzDAOImpl.getInstance().getRelease(rmbid);
	}
}
